﻿using System.Collections.Generic;
using System.Data;
using System;

namespace ZHDJ.MobileInterface
{
    public partial class GetPartyQuestionNaireResult : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            GetPartyQuestionNaireResultOutParam result = new GetPartyQuestionNaireResultOutParam();
            GetPartyQuestionNaireResultInParam param = new GetPartyQuestionNaireResultInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as GetPartyQuestionNaireResultInParam;

                if (string.IsNullOrEmpty(param.Survey_id))
                {
                    result.code = "0";
                    result.desc = "问卷ID不能为空";

                    isFlag = false;
                }

                if (isFlag)
                {
                    DataSet ds = InterfaceHelper.GetPartyQuestionNaireResult(param.Survey_id);
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        PartyQuestionNaireResult itemTemp;
                        //result.data.total_pages = 0;
                        //result.data.total_pages = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            itemTemp = new PartyQuestionNaireResult();
                            itemTemp.Title_Id = dr["Title_Id"].ToString();
                            itemTemp.Title_Content = dr["TopicContent"].ToString();
                            DataSet dsOption = InterfaceHelper.GetPartyQuestionNaireResultOption(param.Survey_id, itemTemp.Title_Id);
                            if (dsOption != null && dsOption.Tables.Count == 2)
                            {
                                int count = Convert.ToInt32(dsOption.Tables[0].Rows[0][0]);
                                foreach (DataRow dr2 in dsOption.Tables[1].Rows)
                                {
                                    string oid = dr2["OptionID"].ToString();
                                    itemTemp.option_ids += dr2["OptionID"].ToString() + ";";
                                    itemTemp.option_Content += dr2["OptionContent"].ToString() + ";";
                                    if (count > 0)
                                    {
                                        DataSet dsOptionC = InterfaceHelper.GetPartyQuestionNaireResultOptionCount(itemTemp.Title_Id, oid);
                                        if (dsOptionC != null && dsOptionC.Tables.Count == 1)
                                        {
                                            itemTemp.option_proportion += (Convert.ToDouble(dsOptionC.Tables[0].Rows[0][0]) * 100 / Convert.ToDouble(count)).ToString("0.00") + "%;";
                                        }
                                    }
                                    else
                                    {
                                        itemTemp.option_proportion += "0%;";
                                    }
                                }
                            }
                            if (itemTemp.option_ids.Length>0)
                            {
                                itemTemp.option_ids = itemTemp.option_ids.Substring(0,(itemTemp.option_ids.Length-1));
                                itemTemp.option_proportion = itemTemp.option_proportion.Substring(0,(itemTemp.option_proportion.Length-1));
                            }
                            result.data.list.Add(itemTemp);
                        }
                    }
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetPartyQuestionNaireResult", SerializeHelper.JsonSerializer<GetPartyQuestionNaireResultInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<GetPartyQuestionNaireResultInParam, GetPartyQuestionNaireResultOutParam>();
            else
                SafeProcessRequest<GetPartyQuestionNaireResultInParam, GetPartyQuestionNaireResultOutParam>();
        }
    }

    #region 请求参数
    public class GetPartyQuestionNaireResultInParam
    {
        /// <summary>
        /// 必填 调查ID
        /// </summary>
        public string Survey_id
        { get; set; }

    }
    #endregion

    #region 结果参数
    public class GetPartyQuestionNaireResultOutParam
    {
        public GetPartyQuestionNaireResultOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new PartyQuestionNaireResultList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public PartyQuestionNaireResultList data
        { get; set; }

    }

    public class PartyQuestionNaireResultList
    {
        public PartyQuestionNaireResultList()
        {
            //total_pages = 0;
            list = new List<PartyQuestionNaireResult>();
        }

        public List<PartyQuestionNaireResult> list { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        //public int total_pages
        //{ get; set; }
    }

    public class PartyQuestionNaireResult
    {
        /// <summary>
        /// 标题ID
        /// </summary>
        public string Title_Id
        { get; set; }

        /// <summary>
        /// 标题内容
        /// </summary>
        public string Title_Content
        { get; set; }

        /// <summary>
        /// 选项ID集合 多选的以英文分号分隔
        /// </summary>
        public string option_ids
        { get; set; }

        /// <summary>
        /// 选项内容
        /// </summary>
        public string option_Content
        { get; set; }

        /// <summary>
        /// 选项占比集合 多选的以英文分号分隔，与ID对应
        /// </summary>
        public string option_proportion
        { get; set; }

    }

    #endregion
}